package com.wangjiacai.hqlQuery.role.controller;


import com.wangjiacai.hqlQuery.role.entity.Function;
import com.wangjiacai.hqlQuery.role.entity.Role;
import com.wangjiacai.hqlQuery.role.service.FunctionService;
import com.wangjiacai.hqlQuery.role.service.RoleService;
import com.wangjiacai.utils.output.ResultVO;
import com.wangjiacai.utils.utils.ResultVOUtil;
import com.wangjiacai.utils.utils.ValidatorUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.data.web.PageableDefault;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;


/*
 * @auther: wangjiacai
 *
 * @create: 2019-01-28 20:10:23
 */
@RestController("roleRootController")
@RequestMapping("/root")
public class RootController {

    @Autowired
    private FunctionService functionService;
    @Autowired
    private RoleService roleService;


    @GetMapping("/function/tree")
    public ResultVO getFunctionTree(){
        return ResultVOUtil.success(functionService.getTreeNode());
    }

    /**
     * 查询权限数
     * @return
     */
    @GetMapping("/function/tree/map")
    public List<Map> getFunctionTreeMap(){
        return functionService.getFunctionTreeMap();
    }


    /**
     * 根据权限id查询权限
     * @param functionId
     * @return
     */
    @GetMapping("/function/{functionId}")
    public ResultVO getFunctionByFunctionId(@PathVariable("functionId") String functionId){
        Function function=functionService.findById(functionId);
        return ResultVOUtil.success(function);
    }

    /**
     * 删除权限
     * @param functionId
     * @return
     */
    @DeleteMapping("/function/{functionId}")
    public ResultVO deleteFunctionByFunctionId(@PathVariable("functionId") String functionId){
        functionService.deleteById(functionId);
        return ResultVOUtil.success();
    }

    /**
     * 创建权限
     * @param function
     * @param bindingResult
     * @return
     */
    @PostMapping("/function")
    public ResultVO postFunction(@Valid  Function function, BindingResult bindingResult){
        ValidatorUtil.valid(bindingResult);
        functionService.addFunction(function);
        return ResultVOUtil.success();
    }

    /**
     * 修改权限
     * @param function
     * @param bindingResult
     * @return
     */
    @PutMapping("/function")
    public ResultVO putFunction(@Valid  Function function, BindingResult bindingResult){
        ValidatorUtil.valid(bindingResult);
        functionService.updateFunction(function);
        return ResultVOUtil.success();
    }

    /**
     * 查询角色树
     * @return
     */
    @GetMapping("/role/tree/map")
    public List<Map> getRoleTreeMap(){
        return roleService.getRoleTreeMap(null);
    }

    /**
     * 分页查询角色
     * @param role
     * @param size
     * @return
     */
    @GetMapping("/role/num")
    public ResultVO getRoleListNum(Role role, Integer size){
        long totalNum=roleService.getRoleListNum(role);
        return ResultVOUtil.success((long)Math.ceil(totalNum/(double)size));
    }

    /**
     * 分页查询角色
     * @param role
     * @param pageable
     * @return
     */
    @GetMapping("/role")
    public ResultVO getRoleList (Role role, @PageableDefault(sort = "updateDateTime") Pageable pageable){
        List<Role> list=roleService.getRoleList(role,pageable);
        return ResultVOUtil.success(list);
    }

    /**
     * 查询全部角色
     * @return
     */
    @GetMapping("/role/all")
    public ResultVO getAllRoleList (){
        List<Role> list=roleService.getAllRoleList();
        return ResultVOUtil.success(list);
    }

    /**
     * 删除角色
     * @param roleId
     * @return
     */
    @DeleteMapping("/role/{roleId}")
    public ResultVO deleteRoleByRoleId (@PathVariable("roleId")String roleId){
        roleService.deleteRole(roleId);
        return ResultVOUtil.success();
    }

    /**
     * 创建角色
     * @param role
     * @param bindingResult
     * @return
     */
    @PostMapping("/role")
    public ResultVO postRole (@Valid  Role role, BindingResult bindingResult){
        ValidatorUtil.valid(bindingResult);
        roleService.addRole(role);
        return ResultVOUtil.success();
    }

    /**
     * 给角色添加权限
     * @param bhs
     * @param roleId
     * @return
     */
    @PutMapping("/role/assignRoleFunction")
    public ResultVO putRoleFunction (String bhs, String roleId){
        roleService.assignRoleFunctionByBhs(bhs,roleId);
        return ResultVOUtil.success();
    }

    /**
     * 查询角色id的权限
     * @param roleId
     * @return
     */
    @GetMapping("/role/{roleId}")
    public ResultVO getBhListByRoleId(@PathVariable("roleId") String roleId){
        List<String> functionIds=functionService.findBhListByRoleId(roleId);
        if(functionIds==null) functionIds=new ArrayList<>();
        return ResultVOUtil.success(functionIds);
    }



    /**
     * 给用户授权角色
     * @param roleIds
     * @param userId
     * @return
     */
    @PutMapping("/user/assignUserRole")
    public ResultVO putUserRole (String roleIds,String userId){
        roleService.assignUserRoleByRoleIds(roleIds,userId);
        return ResultVOUtil.success();
    }

    /**
     * 查看用户的角色
     * @param userId
     * @return
     */
    @GetMapping("/user/{userId}")
    public ResultVO getRoleIdListByUserId(@PathVariable("userId") String userId){
        List<String> roleIds=roleService.findRoleIdListByUserId(userId);
        if(roleIds==null) roleIds=new ArrayList<>();
        return ResultVOUtil.success(roleIds);
    }

}
